package cn.itcast.provider.controller;

import cn.itcast.provider.domain.Goods;
import cn.itcast.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @Value("${server.port}")
    int port;

    @GetMapping("/findById/{id}")
    @HystrixCommand(fallbackMethod = "findById_fallback",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })   //熔断器设置,报错----超时
    public Goods findById(@PathVariable("id") int id){
        //int i = 1/0;
//        try {
//            Thread.sleep(1500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        //id为1异常
        if (id==1){
            int i  =1/0;
        }
        Goods goods = goodsService.findById(id);
        goods.setTitle(port+"");
        return goods;
    }


    public Goods findById_fallback(int id){
        Goods goods = new Goods();
        goods.setId(-1);
        goods.setTitle("提供方降级");
        goods.setPrice(999999);
        return goods;
    }
}
